package com.zsjt.app.mapper;

import com.zsjt.app.pojo.PermissionDto;
import com.zsjt.app.pojo.UserDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("accountMapper")
public interface AccountMapper {
    /**
     * 根据用户名查询用户信息
     * @param account
     * @return
     */
    @Results(id = "accountMap",value = { //关系映射
            @Result(id=true,column = "user_id",property = "id"),
            @Result(column = "user_type",property = "type"),
            @Result(column = "user_name",property = "name"),
            @Result(column = "user_account",property = "account"),
            @Result(column = "user_email",property = "email"),
            @Result(column = "user_phone",property = "phone"),
            @Result(column = "user_password",property = "password"),
    })
    @Select("select * from t_user where user_account=#{account} or user_email=#{account} or user_phone=#{account}")
    public UserDto findUserDtoByUserAccount(@Param("account") String account);

    /**
     * 根据id查询用户信息
     * @param userId
     * @return
     */
    @Select("select * from t_user where user_id = #{userId}")
    @ResultMap(value = "accountMap")
    public  UserDto findUserById(@Param("userId") Integer userId);

    /**
     * 根据用户的id查询用户的权限
     * @param userId
     * @return
     */
    @Select("SELECT * " +
            "FROM t_permission " +
            "WHERE id IN(SELECT permission_id " +
            "FROM t_role_permission " +
            "WHERE role_id IN(SELECT role_id " +
            "FROM t_user_role " +
            "WHERE user_id = #{userId}))")
    public List<PermissionDto> findUserPermissionByUserId(@Param("userId") Integer userId);
}
